/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller;

import com.persistencia.domain.Direccionescuela;
import com.persistencia.domain.Plancurricular;
import com.service.PlanCurricularService;
import com.service.impl.PlanCurricularServiceImpl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

/**
 *
 * @author SHERLY
 */
@ManagedBean
@ViewScoped
public class PlanEstudiosController implements Serializable{
    
    private Plancurricular planCurricular;
    private Plancurricular planCurricularSeleccionado;
    private List<Plancurricular> listaPlanCurricular;
    private PlanCurricularService planCurricularService;
    private String nFctId;
    private String NDrcEscId;
    private boolean enabled;
    
    public PlanEstudiosController() {
        planCurricular = new Plancurricular();
        planCurricularSeleccionado = new Plancurricular();
        listaPlanCurricular = new ArrayList<Plancurricular>();
        planCurricularService = new PlanCurricularServiceImpl();
        nFctId = "0";
        NDrcEscId = "0";       
        listarPlanCurricular();
    }
    
    private void listarPlanCurricular(){
        try{
            System.out.println("Entramos a listar PlanCurricular");
            listaPlanCurricular = planCurricularService.obtenerListaPlanCurricular();           
        } catch (Exception ex) {
            Logger.getLogger(DocenteController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void consultarPlanCurricular(){
        try{
            listarPlanCurricular();
        } catch (Exception ex) {
            Logger.getLogger(PlanEstudiosController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void grabarPlanCurricular(){
        FacesMessage msg = null;
        try{
            System.out.println("Entramos a crear Plan Curricular");
            planCurricularService.registrarPlanCurricular(planCurricular);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO,"Inserción","El Plan Curricular se creo satisfactoriamente.");
            planCurricular = new Plancurricular();
            listarPlanCurricular();
        } catch (Exception ex) {
            Logger.getLogger(PlanEstudiosController.class.getName()).log(Level.SEVERE, null, ex);
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Inserción","No se registro el Plan Curricular.");
        }
        finally{            
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
    
    public SelectItem[] getItemsPlanCurricularSelectOne(){
        try {
            System.out.println("getItemsPlanCurricularSelectOne");
            System.out.println("nDrcEscId="+NDrcEscId);
            if( NDrcEscId.equals("0") ){
                setEnabled(true);
                System.out.println("es = 0,setEnabled"+isEnabled());            
                return null;
            }else{
                setEnabled(false);
                System.out.println("es != 0,setEnabled"+isEnabled());
                return getSelectItems(planCurricularService.obtenerListaPlanCurricularPorDirEscuela(new Direccionescuela(Integer.parseInt(NDrcEscId)) ));
            }
            
        } catch (Exception ex) {
            System.out.println("ERROR en getItemsPlanCurricularSelectOne, ex="+ex.getMessage());
            return null;
        }
        
    }
    
    private SelectItem[] getSelectItems(List<Plancurricular> listaPlanCurricular) {
        SelectItem[] items = new SelectItem[listaPlanCurricular.size()];
        int i = 0;
        for (Plancurricular objPlanCurricular : listaPlanCurricular) {
            items[i++] = new SelectItem(objPlanCurricular.getNPlnCrrId(),objPlanCurricular.getVNombre());
        }
        return items;
    }
    
    public void updateValor(){
        setNDrcEscId("0");
    }
    
    public List<Plancurricular> getListaPlanCurricular() {
        return listaPlanCurricular;
    }

    public void setListaPlanCurricular(List<Plancurricular> listaPlanCurricular) {
        this.listaPlanCurricular = listaPlanCurricular;
    }

    public Plancurricular getPlanCurricular() {
        return planCurricular;
    }

    public void setPlanCurricular(Plancurricular planCurricular) {
        this.planCurricular = planCurricular;
    }

    public Plancurricular getPlanCurricularSeleccionado() {
        return planCurricularSeleccionado;
    }

    public void setPlanCurricularSeleccionado(Plancurricular planCurricularSeleccionado) {
        this.planCurricularSeleccionado = planCurricularSeleccionado;
    }

    public PlanCurricularService getPlanCurricularService() {
        return planCurricularService;
    }

    public void setPlanCurricularService(PlanCurricularService planCurricularService) {
        this.planCurricularService = planCurricularService;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public String getNDrcEscId() {
        return NDrcEscId;
    }

    public void setNDrcEscId(String NDrcEscId) {
        this.NDrcEscId = NDrcEscId;
    }

    public String getNFctId() {
        return nFctId;
    }

    public void setNFctId(String nFctId) {
        this.nFctId = nFctId;
    }
                
}